package com.hex.liyue.commonmodule.manager;

import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;


import com.hex.liyue.commonmodule.daomodule.DaoMaster;
import com.hex.liyue.commonmodule.daomodule.DaoSession;
import com.hex.liyue.commonmodule.daomodule.Friend;
import com.hex.liyue.commonmodule.daomodule.FriendDao;
import com.hex.liyue.commonmodule.daomodule.Group;
import com.hex.liyue.commonmodule.daomodule.GroupDao;

import java.util.List;

/**
 * Created by yukuo on 2016/5/19.
 * 这是一个数据库的管理类
 */
public class DaoManager {
    private final DaoSession daoSession;
    private Activity activity;
    private static DaoManager daoManager;

    /**
     * 获取一个我的好友的dao
     *
     * @return
     */
    public FriendDao getFriedDao() {
        return daoSession.getFriendDao();
    }

    /**
     * 获取我的分组的dao
     *
     * @return
     */
    public GroupDao getGroupDao() {
        return daoSession.getGroupDao();
    }

    /**
     * 更新本地的分组数据库
     *
     * @param list
     */
    public void insertOrUpdataGroup(final List<Group> list) {
        for (Group group : list) {
            getGroupDao().insertOrReplaceInTx(group);
        }
    }

    /**
     * 更新本地的好友数据库
     *
     * @param list
     */
    public void insertOrUpdataFriends(final List<Friend> list) {
        new Thread() {
            @Override
            public void run() {
                for (Friend friend : list) {
                    getFriedDao().insertOrReplaceInTx(friend);
                }
            }
        }.start();
    }

    /**
     * 获取一个好友
     *
     * @param friendid
     * @return
     */
    public Friend getFriend(long friendid) {
        Friend friend = getFriedDao().load(friendid);
        return friend;
    }

    /**
     * 获取一个分组
     *
     * @param groupid
     * @return
     */
    public Group getGroup(long groupid) {
        Group group = getGroupDao().load(groupid);
        return group;
    }

    /**
     * 获取所有的好友列表
     *
     * @return
     */
    public List<Friend> getFriends() {
        List<Friend> friend = getFriedDao().queryBuilder().list();
        return friend;
    }

    /**
     * 获取某个分组下的所有好友
     *
     * @param groupid 分组id
     * @return
     */
    public List<Friend> getGroupFriends(String groupid) {
        List<Friend> list = getFriedDao().queryBuilder()
                .where(FriendDao.Properties.Group_id.eq(groupid))
                .list();
        return list;
    }

    /**
     * 获取所有的分组列表
     *
     * @return
     */
    public List<Group> getGroups() {
        List<Group> groups = getGroupDao().queryBuilder().list();
        return groups;
    }

    private DaoManager(Activity activity) {
        this.activity = activity;
        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(activity, "4.db", null);
        SQLiteDatabase db = helper.getWritableDatabase();
        // 注意：该数据库连接属于 DaoMaster，所以多个 Session 指的是相同的数据库连接。
        DaoMaster daoMaster = new DaoMaster(db);
        daoSession = daoMaster.newSession();
    }

    public static DaoManager getInstance(Activity activity) {
        if (daoManager == null) {
            synchronized (DaoManager.class) {
                if (daoManager == null) {
                    daoManager = new DaoManager(activity);
                }
            }
        }
        return daoManager;
    }
}
